package com.jintian.smart.kernel.core.domain;

import lombok.Setter;
import org.beetl.sql.core.page.DefaultPageRequest;
import org.beetl.sql.core.page.PageRequest;
import org.beetl.sql.core.page.PageResult;
import org.beetl.sql.core.query.Query;

import java.util.Collections;
import java.util.List;

/**
 * 简单分页模型
 */
public class Page<T> implements IPage<T> {
    private static final long serialVersionUID = 8545996863226528798L;

    /**
     * 查询数据列表
     */
    protected List<T> datas = Collections.emptyList();

    /**
     * 总数
     */
    protected long total = 0;

    /**
     * 每页显示条数，默认 10
     */
    protected int size = 10;

    /**
     * 当前页
     */
    protected long current = 1;

    /**
     * 总页数
     */
    @Setter
    protected long pages = 0;

    /**
     * 无参分页构造函数
     */
    public Page() {
    }

    /**
     * 分页构造函数
     *
     * @param current 当前页
     * @param size    每页显示条数
     */
    public Page(long current, int size) {
        this(current, size, 0);
    }

    /**
     * 分页构造函数
     *
     * @param current 当前页
     * @param size    每页显示条数
     * @param total   数据总数
     */
    public Page(long current, int size, long total) {
        if (current > 1) {
            this.current = current;
        }
        this.size = size;
        this.total = total;
    }

    /**
     * 分页构造函数
     */
    public Page(PageRequest request, PageResult<T> result) {
        this.setCurrent(request.getPageNo());
        if (request.getPageSize() <= 0) {
            this.setSize(result.getTotalRow().intValue());
            this.setPages(1);
        } else {
            this.setSize(request.getPageSize());
            this.setPages(result.getTotalPage());
        }
        this.setTotal(result.getTotalRow());
        this.setList(result.getList());
    }

    /**
     * 是否存在上一页
     */
    public boolean hasPrevious() {
        return this.current > 1;
    }

    /**
     * 是否存在下一页
     */
    public boolean hasNext() {
        return this.current < this.getPages();
    }

    /**
     * 获取数据列表
     */
    public List<T> getList() {
        return this.datas;
    }

    /**
     * 设置数据列表
     */
    public void setList(List<T> datas) {
        this.datas = datas;
    }

    @Override
    public long getTotal() {
        return this.total;
    }

    @Override
    public Page<T> setTotal(long total) {
        this.total = total;
        return this;
    }

    @Override
    public long getSize() {
        return this.size;
    }

    @Override
    public Page<T> setSize(int size) {
        this.size = size;
        return this;
    }

    @Override
    public long getCurrent() {
        return this.current;
    }

    @Override
    public Page<T> setCurrent(long current) {
        this.current = current;
        return this;
    }

    public PageRequest<T> toPageRequest() {
        return DefaultPageRequest.of(this.current, this.size);
    }


    //region 静态方法
    public static <T> Page<T> of(long current, int size) {
        return of(current, size, 0);
    }

    public static <T> Page<T> of(long current, int size, long total) {
        return new Page<>(current, size, total);
    }

    public static <T> IPage<T> getPage(PageRequest request, PageResult<T> result) {
        Page<T> page = new Page<>();
        page.setCurrent(request.getPageNo());
        if (request.getPageSize() <= 0) {
            page.setSize(result.getTotalRow().intValue());
            page.setPages(1);
        } else {
            page.setSize(request.getPageSize());
            page.setPages(result.getTotalPage());
        }
        page.setTotal(result.getTotalRow());
        page.setList(result.getList());
        return page;
    }
    //endregion
}
